package edu.gxnu.cetcdemo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import edu.gxnu.cetcdemo.mapper.TOpeningMapper;
import edu.gxnu.cetcdemo.pojo.TOpening;
import edu.gxnu.cetcdemo.service.TOpeningService;
import edu.gxnu.cetcdemo.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Uplifted
 * @description 针对表【t_duty】的数据库操作Service实现
 * @createDate 2024-04-02 15:09:11
 */
@Service
public class TOpeningServiceImpl extends ServiceImpl<TOpeningMapper, TOpening>
        implements TOpeningService{

    @Autowired
    private TOpeningMapper tOpeningMapper;

    @Override
    public Result queryTOpening() {
        Map data = new HashMap();
        List<TOpening> list = tOpeningMapper.selectList(null);
        data.put("tOpeningList", list);
        return Result.build(200, "成功查询所有信息", data);
    }

    @Override
    public Result addTOpening(TOpening tOpening) {
        // 是否存在相同信息的记录
        LambdaQueryWrapper<TOpening> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(TOpening::getOpeningName, tOpening.getOpeningName())
                .eq(TOpening::getOpeningPhone, tOpening.getOpeningPhone())
                .eq(TOpening::getOpeningTime, tOpening.getOpeningTime())
                .eq(TOpening::getOpeningWeek, tOpening.getOpeningWeek());
        TOpening tTOpening = tOpeningMapper.selectOne(lambdaQueryWrapper);

        if(tTOpening != null){
            return Result.build(501, "该信息已存在");
        }

        tOpeningMapper.insert(tOpening);
        return Result.build(200, "成功添加信息");
    }

    @Override
    public Result updateTOpening(TOpening tOpening) {
        // 是否存在相同信息的记录
        LambdaQueryWrapper<TOpening> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(TOpening::getOpeningName, tOpening.getOpeningName())
                .eq(TOpening::getOpeningPhone, tOpening.getOpeningPhone())
                .eq(TOpening::getOpeningTime, tOpening.getOpeningTime())
                .eq(TOpening::getOpeningWeek, tOpening.getOpeningWeek());
        TOpening tTOpening = tOpeningMapper.selectOne(lambdaQueryWrapper);

        if(tTOpening != null){
            return Result.build(501, "存在相同信息");
        }

        tOpeningMapper.update(tOpening, new LambdaQueryWrapper<TOpening>().eq(TOpening::getOpeningId, tOpening.getOpeningId()));
        return Result.build(200, "成功更新信息");
    }

    @Override
    public Result delTOpening(long id) {
        LambdaQueryWrapper<TOpening> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        tOpeningMapper.delete(lambdaQueryWrapper.eq(TOpening::getOpeningId, id));
        return Result.build(200, "成功删除信息");
    }
}




